<?php
/*********************************************
  CPG Dragonfly™ CMS
  ********************************************
  Copyright © 2004 - 2005 by CPG-Nuke Dev Team
  http://www.dragonflycms.com

  Dragonfly is released under the terms and conditions
  of the GNU GPL version 2 or any later version
**********************************************/

class Surveys_Poll
{
	protected
		$id        = 0,
		$title     = '',
		$anonymous = false,
		$comments  = false,
		$ptime     = 0,
		$votes     = 0,
		$artid     = 0,
		$language  = '',

		$results = array();

	function __construct($id=0)
	{
		$id = intval($id);
		if ($id)
		{
			$K    = Dragonfly::getKernel();
			$db   = $K->SQL;
			$L10N = $K->L10N;

			$poll = $db->uFetchAssoc("SELECT
				poll_title,
				poll_ptime,
				anonymous,
				comments,
				voters,
				planguage,
				artid
			FROM {$db->TBL->poll_desc}
			WHERE poll_id={$id}");
			if (!$poll)
			{
				throw new Exception(sprintf(_ERROR_NONE_TO_DISPLAY, strtolower(_SurveysLANG)));
			}

			$this->id        = $id;
			$this->title     = $poll['poll_title'];
			$this->anonymous = !!$poll['anonymous'];
			$this->comments  = ($poll['comments'] && $K->CFG->global->pollcomm);
			$this->ptime     = intval($poll['poll_ptime']);
			$this->votes     = intval($poll['voters']);
			$this->artid     = intval($poll['artid']);
			$this->language  = $poll['planguage'];

			$this->votes_txt    = $L10N->plural($this->votes,'%d votes');

			# if you are anonymous and no anonymous votes allowed, you can't vote
			if (!is_user()) {
				if ($this->anonymous) {
					// only one anonymous per ip
					$this->voted = $db->count('poll_check', "user_id=0 AND ip={$K->IDENTITY->ip} AND poll_id={$this->id}");
				} else {
					$this->voted = 1;
				}
			} else {
				$this->voted = $db->count('poll_check', "user_id={$K->IDENTITY->id} AND poll_id={$this->id}");
			}

			$result = $db->query("SELECT
				vote_id id,
				option_text label,
				option_count votes
			FROM {$db->TBL->poll_data}
			WHERE poll_id={$id} AND option_text!=''
			ORDER BY option_count DESC, option_text");
			$rate = null;
			while ($row = $result->fetch_assoc()) {
				$row['value']   = ($this->votes ? $row['votes'] / $this->votes : 0);
				$row['percent'] = $L10N->round($row['value'] * 100, 1).'%';
				if (null === $rate) $rate = $row['value'];
				$row['rate'] = min(1,$row['value']/$rate);
				$this->results[$row['id']] = $row;
			}
			$result->free();
		}
	}

	function __get($k)
	{
		if ('options' === $k) {
			$options = $this->results;
			ksort($options);
			return $options;
		}
		if ('results' === $k) {
			return $this->results;
		}
		if (!isset($this->comments_count) && ('comments_count' === $k || 'comments_count_txt' === $k)) {
			if ($this->comments) {
				$this->comments_count = Dragonfly::getKernel()->SQL->count('pollcomments', "poll_id={$this->id}");
			} else {
				$this->comments_count = 0;
			}
		}
		if ('comments_count_txt' === $k) {
			$this->comments_count_txt = Dragonfly::getKernel()->L10N->plural($this->comments_count, '%d comments');
		}

		return $this->$k;
	}

}
